package by.yakusekmaxim.bookgid.webapp.page.home.book;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Book_;
import by.yakusekmaxim.bookgid.webapp.page.HomeLayout;
import by.yakusekmaxim.bookgid.webapp.page.home.book.FeedbackAndReadPanel;

public class BookDetailsPage extends HomeLayout {

	private Book book;

	protected void onInitialize() {
		super.onInitialize();
		

		Label imageLabel = new Label("imageLabel", "");
		if (book.getImage() != null) {
			imageLabel.add(AttributeModifier.replace("src", book.getImage()
					.getUrl()));
		}
		add(imageLabel);

		add(new FeedbackAndReadPanel("feedbackAndReadPanel",book));

		Label nameLabel = new Label("name", book.getName());
		add(nameLabel);

		Label nameOrLabel = new Label("nameOr", book.getNameOr());
		add(nameOrLabel);

		Label dateLabel = new Label("date", getDataFormat(book.getDate()));
		add(dateLabel);

		Link<Object> linkAuthor = new Link<Object>("linkAuthor") {
			@Override
			public void onClick() {
				Filter<Book> filter = new Filter<Book>();
				filter.setAttribute(Book_.author);
				filter.setValue(book.getAuthor());
				filter.setEnabled(true);
				BookPage page = new BookPage(filter, new ResourceModel(
						"p.admin.author.bookLink").getObject()
						+ book.getAuthor().getName());
				setResponsePage(page);

			}
		};

		linkAuthor.setEnabled(book.getAuthor() != null);

		linkAuthor.add(new Label("author", getAuthorName(book)));
		add(linkAuthor);

		Link<Object> linkGenre = new Link<Object>("linkGenre") {
			@Override
			public void onClick() {
				Filter<Book> filter = new Filter<Book>();
				filter.setAttribute(Book_.genre);
				filter.setValue(book.getGenre());
				filter.setEnabled(true);
				BookPage page = new BookPage(filter, new ResourceModel(
						"p.admin.genre.bookLink").getObject()
						+ book.getGenre().getName());
				setResponsePage(page);

			}
		};
		linkGenre.setEnabled(book.getGenre() != null);
		Label genreLabel = new Label("genre", getGenreName(book));
		linkGenre.add(genreLabel);
		add(linkGenre);

		Link<Object> linkSeries = new Link<Object>("linkSeries") {
			@Override
			public void onClick() {
				Filter<Book> filter = new Filter<Book>();
				filter.setAttribute(Book_.series);
				filter.setValue(book.getSeries());
				filter.setEnabled(true);
				BookPage page = new BookPage(filter, new ResourceModel(
						"p.admin.series.bookLink").getObject()
						+ book.getSeries().getName());
				setResponsePage(page);

			}
		};
		linkSeries.setEnabled(book.getSeries() != null);
		Label seriesLabel = new Label("series", getSeriesName(book));
		linkSeries.add(seriesLabel);
		add(linkSeries);

		Label descriptionLabel = (Label) new Label("description",
				book.getDescription()).setEscapeModelStrings(false);
		add(descriptionLabel);
		
		add(new NewReviewPanel("newReviewPanel",book));
		
		add(new ReviewListPanel("reviewListPanel",book));
	}

	public BookDetailsPage(final Book book) {
		this.book = book;
	}

	private String getDataFormat(Date date) {
		if (date == null) {
			return "-";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		return dateFormat.format(date);
	}

	private String getAuthorName(Book book) {
		if (book.getAuthor() == null) {
			return "-";
		}
		return book.getAuthor().getName();
	}

	private String getGenreName(Book book) {
		if (book.getGenre() == null) {
			return "-";
		}
		return book.getGenre().getName();
	}

	private String getSeriesName(Book book) {
		if (book.getSeries() == null) {
			return "-";
		}
		return book.getSeries().getName();
	}

	@Override
	protected MarkupContainer addPanel() {

		return null;
	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.home.bookDetailsPage");
	}
}
